package service

import (
	"boy-go/modules/system/model"
	"boy-go/modules/system/repository"
	"boy-go/modules/system/vo"
	"boy-go/pkg/request"
	"github.com/jinzhu/copier"
)

func NewSysDictDataService() SysDictDataService {
	return &sysDictDataService{
		SysDictDataRepo: repository.NewSysDictDataRepository(),
	}
}

type sysDictDataService struct {
	SysDictDataRepo *repository.SysDictDataRepository
}

// GetById 根据id获取配置信息
func (s *sysDictDataService) GetById(dictCode int64) (*vo.SysDictDataModel, error) {
	m, err := s.SysDictDataRepo.GetById(dictCode)
	if err != nil {
		return nil, err
	}
	rsp := new(vo.SysDictDataModel)
	if err := copier.Copy(rsp, m); err != nil {
		return nil, err
	}
	return rsp, nil
}

// List 获取字典数据表列表
func (s *sysDictDataService) List(req *vo.QuerySysDictDataReq) (*vo.ListSysDictDataRsp, error) {
	var pageReq = &request.PageRequest{
		PageNum:  req.PageNum,
		PageSize: req.PageSize,
	}
	list, total, err := s.SysDictDataRepo.List(req, pageReq)
	if err != nil {
		return nil, err
	}
	itemList := make([]*vo.SysDictDataModel, 0)
	for _, v := range list {
		item := new(vo.SysDictDataModel)
		if err := copier.Copy(item, &v); err != nil {
			continue
		}
		itemList = append(itemList, item)
	}
	return &vo.ListSysDictDataRsp{
		List:  itemList,
		Total: total,
	}, nil
}

// Add 添加字典数据表
func (s *sysDictDataService) Add(req *vo.AddSysDictDataReq) (*vo.AddSysDictDataRsp, error) {
	m := new(model.SysDictData)
	err := copier.Copy(m, req)
	if err != nil {
		return nil, err
	}
	if err := s.SysDictDataRepo.Save(m); err != nil {
		return nil, err
	}
	return &vo.AddSysDictDataRsp{
		DictCode: m.DictCode,
	}, nil
}

// Edit 修改字典数据表
func (s *sysDictDataService) Edit(req *vo.EditSysDictDataReq) (*vo.EditSysDictDataRsp, error) {
	m := new(model.SysDictData)
	err := copier.Copy(m, req)
	if err != nil {
		return nil, err
	}
	if err := s.SysDictDataRepo.Update(m); err != nil {
		return nil, err
	}
	return &vo.EditSysDictDataRsp{
		DictCode: m.DictCode,
	}, nil
}

// Del 删除字典数据表
func (s *sysDictDataService) Del(dictCode int64) error {
	return s.SysDictDataRepo.Delete(dictCode)
}

func (s *sysDictDataService) BatchDel(ids []string) error {
	return s.SysDictDataRepo.BatchDelete(ids)
}
